package livechat;

import java.io.IOException;
import javax.servlet.http.*;
import java.util.List;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;


@SuppressWarnings("serial")
public class TestServlet extends HttpServlet{
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
		req.setCharacterEncoding("UTF-8");
		
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

        long lTime = Long.parseLong(req.getParameter("time"));
        
        Query query = new Query("chatlog");
        query.addFilter("time", Query.FilterOperator.GREATER_THAN, lTime);
        query.addSort("time", Query.SortDirection.ASCENDING);
        List<Entity> results = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
        resp.setContentType("text/plain");
       
        for (Entity eChatLog : results) {
        	 resp.getWriter().println("[" + eChatLog.getProperty("time") + "]" + eChatLog.getProperty("username") + ": " + eChatLog.getProperty("message"));
		}

    }
}
